<template>
  <div class="box">
    <header class="header">
      <van-nav-bar
        :title="totalNum > 0 ? '购物车(' + totalNum + ')' : '购物车' "
        left-arrow
        @click-left="$router.back()"
      />
    </header>
    <div class="content">
      <div class="noShop" v-if="empty">
        <van-empty
        image="https://img1.baidu.com/it/u=812865506,616477484&fm=253&fmt=auto&app=120&f=JPEG"
        description="购物车空空如也">
          <van-button round type="danger" @click="$router.push('/kind')" class="bottom-button">立即购买</van-button>
        </van-empty>
      </div>
      <div class="hasShop" v-else>
        <van-swipe-cell v-for="item of cartList" :key="item.cartid">
          <van-row>
            <van-col span="2" class="checkBox">
              <van-checkbox @change="updateFlag(item.cartid, item.flag)" v-model="item.flag"></van-checkbox>
            </van-col>
            <van-col span="22">
              <van-card
                :price="item.originprice"
                :title="item.proname"
                :thumb="item.img1"
              >
                <template #num>
                  <van-stepper @change="updateNumFn(item.cartid, item.num)" theme="round" button-size="22" v-model="item.num" />
                </template>
              </van-card>
            </van-col>
          </van-row>

          <template #right>
            <van-button square @click="deleteItem(item.cartid)" text="删除" type="danger" class="delete-button" />
          </template>
        </van-swipe-cell>
        <van-submit-bar :disabled="totalNum === 0" :price="totalPrice" button-text="提交订单" @submit="onSubmit">
          <van-checkbox @click="selectAllFn" v-model="checked">全选</van-checkbox>
        </van-submit-bar>
      </div>
      <van-divider>猜你喜欢</van-divider>
      <ProList :proList="recommendList"/>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import { Divider, Col, Row, NavBar, Empty, Button, Card, Stepper, SwipeCell, SubmitBar, Checkbox } from 'vant'
import { getCartList, remove, updateNum, selectall, selectone, getRecommendList } from './../../api/cart'
import { addOrder } from '@/api/order'
import ProList from './../../components/ProList'
Vue.use(Divider)
Vue.use(NavBar)
Vue.use(Empty)
Vue.use(Button)
Vue.use(Card)
Vue.use(Stepper)
Vue.use(SwipeCell)
Vue.use(SubmitBar)
Vue.use(Checkbox)
Vue.use(Col)
Vue.use(Row)
export default {
  components: {
    ProList
  },
  data () {
    return {
      empty: true,
      cartList: [],
      recommendList: [],
      checked: false // 全选的选中状态
    }
  },
  computed: {
    totalPrice () {
      return this.cartList.reduce((sum, item) => {
        return item.flag ? sum + item.originprice * item.num : sum + 0
      }, 0) * 100
    },
    totalNum () {
      return this.cartList.reduce((sum, item) => {
        return item.flag ? sum + item.num : sum + 0
      }, 0)
    }
  },
  mounted () {
    if (localStorage.getItem('isLogin') === 'true') {
      this.getCartListData()
    } else {
      this.$router.push('/login')
    }
    getRecommendList().then(res => {
      this.recommendList = res.data.data
    })
  },
  methods: {
    updateFlag (cartid, flag) {
      console.log(cartid, flag)
      selectone({ cartid, flag }).then(() => { this.getCartListData() })
    },
    selectAllFn () {
      // 更新数据库，重新请求一次数据
      selectall({
        userid: localStorage.getItem('userid'),
        type: this.checked
      }).then(() => {
        this.getCartListData()
      })
    },
    updateNumFn (cartid, num) {
      // console.log(cartid, num)
      updateNum({
        cartid, num
      }).then(() => {
        this.getCartListData()
      })
    },
    onSubmit () {
      // 点击提交订单跳转到确认订单页面
      console.log('确认订单')
      addOrder({
        userid: localStorage.getItem('userid')
      }).then(res => {
        console.log(res.data)
        this.$router.push('/order/' + res.data.time)
      })
    },
    getCartListData () {
      getCartList({
        userid: localStorage.getItem('userid')
      }).then(res => {
        // console.log(res.data)
        if (res.data.code === '10020') {
          this.empty = true
        } else {
          this.empty = false
          this.cartList = res.data.data
          // 判断全选是不是被选中
          this.checked = this.cartList.every(item => item.flag)
        }
      })
    },
    deleteItem (cartid) {
      remove({ cartid }).then(() => {
        this.getCartListData()
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
.goods-card {
  margin: 0;
  background-color: #f66;
}

.delete-button {
  height: 100%;
}
.van-row
  background-color #fff
.van-card
  background-color #fff
.checkBox
  display flex
  justify-content center
  align-items center
  height 100%
  background-color #fff
  .van-checkbox
    position absolute
    top 50%
    transform translateY(-50%)

</style>
